package com.wlit.alarm.utils;

import org.apache.log4j.Logger;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class SmsUtils {

    private static Logger logger = Logger.getLogger(SmsUtils.class);
    /**
     * 构建短信
     *
     * @param phoneNumber
     * @param smsString
     * @param bSmsType
     * @return
     * @throws Exception
     */
    public static byte[] buildSms(String phoneNumber, String smsString, int bSmsType) {
        int _msgId = 0;
        //发送短信
        if (smsString.getBytes().length > 140) {
            System.out.println("短信内容超长，最多70个汉字");
            return null;
        }
        byte[] b = new byte[180];
        int index = 0;
        b[index++] = (byte) 0x7b;
        b[index++] = (byte) 0x7b;
        b[index++] = (byte) 0x7b;
        //功能码
        b[index++] = (byte) 0xff;
        //数据帧长度
        index += 2; //跳过
        //命令类型
        b[index++] = (byte) 0x08;
        //短信格式
        b[index++] = (byte) bSmsType;  //短信类型
        //电话号码长度
        b[index++] = (byte) phoneNumber.length();

        byte[] tempByteBuf = phoneNumber.getBytes(StandardCharsets.US_ASCII);
        System.arraycopy(tempByteBuf, 0, b, index, tempByteBuf.length);
        index += tempByteBuf.length;


        try {
            if (bSmsType == 4) {
                tempByteBuf = smsString.getBytes("gb2312");
            } else if (bSmsType == 2) {
                tempByteBuf = smsString.getBytes(StandardCharsets.ISO_8859_1);
            } else if (bSmsType == 1) {
                tempByteBuf = stringToUnicodeBytes(smsString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //内容长度
        b[index++] = (byte) tempByteBuf.length;
        System.arraycopy(tempByteBuf, 0, b, index, tempByteBuf.length);
        index += tempByteBuf.length;

        b[index++] = 1; //返回发送结果
        //2字节短信流水号
        b[index++] = (byte) (_msgId / 0x100);
        b[index++] = (byte) (_msgId % 0x100);
        _msgId++;

        int ckSumIndex = index++;
        //包尾
        b[index++] = (byte) 0x7d;
        b[index++] = (byte) 0x7d;
        b[index++] = (byte) 0x7d;

        //填充总包长
        b[4] = (byte) (index / 0x100);
        b[5] = (byte) (index % 0x100);

        //填充校验和
        byte ckSum = 0;
        for (int i = 0; i < (index - 4); i++) {
            ckSum ^= b[i];
        }
        b[ckSumIndex] = ckSum;
        //新建一个，然后复制到新数组
        byte[] result = new byte[index];
        System.arraycopy(b, 0, result, 0, index);
        //然后串口发送
        return result;
    }


    private static byte[] stringToUnicodeBytes(String srcText) {
        if (srcText == "") return null;
        try {
            byte[] tempBuf = srcText.trim().getBytes("UTF-16LE");
            byte[] returnBuf = new byte[tempBuf.length];
            for (int i = 0; i < tempBuf.length; i += 2) {
                returnBuf[i] = tempBuf[i + 1];
                returnBuf[i + 1] = tempBuf[i];
            }

            // 截取实际交换后的有效字节（对于偶数长度的字符串无需此步）
            int actualLength = tempBuf.length / 2 * 2;
            byte[] trimmedReturnBuf = Arrays.copyOf(returnBuf, actualLength);

            return trimmedReturnBuf;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] buildVoice(String playPhone, String playContent) {
        //默认播放3次
        int playtimes = 3;

        byte[] sendBytes = new byte[200];
        int index = 0;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        //功能码
        sendBytes[index++] = (byte) 0xfe;
        //数据帧长度
        index += 2; //跳过
        //命令类型
        sendBytes[index++] = (byte) 0x0f;
        //======================
        //播放多少遍内容
        sendBytes[index++] = (byte) playtimes;
        //电话号码长度
        byte[] tempByteBuf = playPhone.getBytes(StandardCharsets.US_ASCII);


        sendBytes[index++] = (byte) tempByteBuf.length;
        System.arraycopy(tempByteBuf, 0, sendBytes, index, tempByteBuf.length);
        index += tempByteBuf.length;
        //内容长度
        try {
            tempByteBuf = stringToUnicodeBytes(playContent);//这里要注意下是unicode
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (tempByteBuf.length > 200) {
            System.out.println("语音内容超长，最多200字节（100个汉字，200个英文)");
            return null;
        }
        sendBytes[index++] = (byte) tempByteBuf.length;
        System.arraycopy(tempByteBuf, 0, sendBytes, index, tempByteBuf.length);
        index += tempByteBuf.length;
        //=======================================
        int ckSumIndex = index++;
        //包尾
        sendBytes[index++] = (byte) 0x7d;
        sendBytes[index++] = (byte) 0x7d;
        sendBytes[index++] = (byte) 0x7d;

        //填充总包长
        sendBytes[4] = (byte) (index / 0x100);
        sendBytes[5] = (byte) (index % 0x100);

        //填充校验和
        byte ckSum = 0;
        for (int i = 0; i < (index - 4); i++) {
            ckSum ^= sendBytes[i];
        }
        sendBytes[ckSumIndex] = ckSum;

        //---------
        byte[] result = new byte[index];
        System.arraycopy(sendBytes, 0, result, 0, index);
        return result;
    }


    /**
     * 立即挂断电话
     *
     * @return
     */
    public static byte[] buildHangup() {
        byte[] sendBytes = new byte[200];
        int index = 0;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        //功能码
        sendBytes[index++] = (byte) 0xfe;
        //数据帧长度
        index += 2; //跳过
        //命令类型
        sendBytes[index++] = 0x0e;
        //======================
        sendBytes[index++] = (byte) 'A';
        sendBytes[index++] = (byte) 'T';
        sendBytes[index++] = (byte) 'H';
        //=======================================
        int ckSumIndex = index++;
        //包尾
        sendBytes[index++] = 0x7d;
        sendBytes[index++] = 0x7d;
        sendBytes[index++] = 0x7d;

        //填充总包长
        sendBytes[4] = (byte) (index / 0x100);
        sendBytes[5] = (byte) (index % 0x100);

        //填充校验和
        byte ckSum = 0;
        for (int i = 0; i < (index - 4); i++) {
            ckSum ^= sendBytes[i];
        }
        sendBytes[ckSumIndex] = ckSum;

        byte[] result = new byte[index];
        System.arraycopy(sendBytes, 0, result, 0, index);
        return result;
    }


    /**
     * 读取当前配置数据
     *
     * @return
     */
    public static byte[] buildConfiguration() {
        byte[] sendBytes = new byte[200];
        int index = 0;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        //功能码
        sendBytes[index++] = (byte) 0xff;
        //数据帧长度
        index += 2; //跳过
        //命令类型
        sendBytes[index++] = 0x00;

        sendBytes[index++] = (byte) ('R');
        sendBytes[index++] = (byte) ('E');
        sendBytes[index++] = (byte) ('A');
        sendBytes[index++] = (byte) ('D');


        int ckSumIndex = index++;
        //包尾
        sendBytes[index++] = (byte) 0x7d;
        sendBytes[index++] = (byte) 0x7d;
        sendBytes[index++] = (byte) 0x7d;

        //填充总包长
        sendBytes[4] = (byte) (index / 0x100);
        sendBytes[5] = (byte) (index % 0x100);

        //填充校验和
        byte ckSum = 0;
        for (int i = 0; i < (index - 4); i++) {
            ckSum ^= sendBytes[i];
        }
        sendBytes[ckSumIndex] = ckSum;
        //-----------
        byte[] result = new byte[index];
        System.arraycopy(sendBytes, 0, result, 0, index);
        return result;
    }

    /**
     * 读取信号强度[0-31,99为未知]
     *
     * @return
     */
    public static byte[] buildRSSI() {
        byte[] sendBytes = new byte[200];
        int index = 0;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        //功能码
        sendBytes[index++] = (byte) 0xff;
        //数据帧长度
        index += 2; //跳过
        //命令类型
        sendBytes[index++] = (byte) 0x06;


        int ckSumIndex = index++;
        //包尾
        sendBytes[index++] = (byte) 0x7d;
        sendBytes[index++] = (byte) 0x7d;
        sendBytes[index++] = (byte) 0x7d;

        //填充总包长
        sendBytes[4] = (byte) (index / 0x100);
        sendBytes[5] = (byte) (index % 0x100);

        //填充校验和
        byte ckSum = 0;
        for (int i = 0; i < (index - 4); i++) {
            ckSum ^= sendBytes[i];
        }
        sendBytes[ckSumIndex] = ckSum;
        //----------
        byte[] result = new byte[index];
        System.arraycopy(sendBytes, 0, result, 0, index);
        return result;
    }

    /**
     * 重启硬件DTU模块
     *
     * @return
     */
    public static byte[] buildReset() {
        byte[] sendBytes = new byte[200];
        int index = 0;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        sendBytes[index++] = (byte) 0x7b;
        //功能码
        sendBytes[index++] = (byte) 0xff;
        //数据帧长度
        index += 2; //跳过
        //命令类型
        sendBytes[index++] = (byte) 0x0b;

        sendBytes[index++] = (byte) ('r');
        sendBytes[index++] = (byte) ('e');
        sendBytes[index++] = (byte) ('s');
        sendBytes[index++] = (byte) ('e');
        sendBytes[index++] = (byte) ('t');

        int ckSumIndex = index++;
        //包尾
        sendBytes[index++] = (byte) 0x7d;
        sendBytes[index++] = (byte) 0x7d;
        sendBytes[index++] = (byte) 0x7d;

        //填充总包长
        sendBytes[4] = (byte) (index / 0x100);
        sendBytes[5] = (byte) (index % 0x100);

        //填充校验和
        byte ckSum = 0;
        for (int i = 0; i < (index - 4); i++) {
            ckSum ^= sendBytes[i];
        }
        sendBytes[ckSumIndex] = ckSum;

        //----------
        byte[] result = new byte[index];
        System.arraycopy(sendBytes, 0, result, 0, index);
        return result;
    }



}
